using BackData;
using Domain.DTOs.ProcessSteps;
using Domain.工艺工序;
using Infratructure;
using MediatR;
using SqlSugar;
using System.Linq;
using Team.API.Applocation.Command.Makesteps;

namespace Team.API.Applocation.Hanldler.Makesteps
{
    /// <summary>
    /// 下拉选项处理器
    /// </summary>
    public class GetMakestepSelectionsHandler : IRequestHandler<GetMakestepSelectionsCommand, APIResult<MakestepSelectOptionsDTO>>
    {
        private readonly IRepository<Makestepmodel> repository;

        public GetMakestepSelectionsHandler(IRepository<Makestepmodel> repository)
        {
            this.repository = repository;
        }

        public async Task<APIResult<MakestepSelectOptionsDTO>> Handle(GetMakestepSelectionsCommand request, CancellationToken cancellationToken)
        {
            // 汇总所有顶级工序用于构建下拉
            var list = await repository.GetAll()
                .Where(x => !x.IsDel && x.Parentid == 0)
                .OrderBy(x => x.Goodsname)
                .ToListAsync();

            var dto = new MakestepSelectOptionsDTO
            {
                GoodsOptions = list.Select(x => x.Goodsname).Where(x => !string.IsNullOrWhiteSpace(x)).Distinct().ToList(),
                VersionOptions = list.Select(x => x.Version).Where(x => !string.IsNullOrWhiteSpace(x)).Distinct().ToList()
            };

            return APIResult<MakestepSelectOptionsDTO>.Ok("获取成功", dto);
        }
    }
}

